/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package library.ucm;

//import com.mysql.jdbc.Blob;
import database.Select;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.sql.Blob;
import javax.swing.ImageIcon;
import library.view.DeliveryDesk;

/**
 *
 * @author Donghui
 */

public class SetDeliveryDeskPhoto {

    private String tppath="D:\\TP_BUFFER\\";
    private String path;
    private static SetDeliveryDeskPhoto local;
    private String tp;
    private String[] spliter;
    private String id;
    private String sql;
    private ResultSet result;
    private File file;
    private byte[] buffer=new byte[4096];

    public static SetDeliveryDeskPhoto getInstance(String tp)
    {
        if(local==null)
        {
            local=new SetDeliveryDeskPhoto();
            local.tp=tp;
        }
        else
        {
            local.tp=tp;
        }
        return local;
    }

    public void setPhoto(){
        spliter=tp.split(";");
        id=spliter[1];
        try {
            InputStream inputimage=null;
            OutputStream outputimage=null;
            sql="SELECT photo FROM roll WHERE id='"+ id +"'";

            result=Select.getInstance().selectValue(sql);
            result.next();
            path=tppath+id+".jpg";
            System.out.println("path: " + path);
            file=new File(path);
            if(!file.exists())
            {
                try{
                 file.createNewFile();
                }
                catch(IOException e)
                {
                e.printStackTrace();
                }
            }
            try{
                outputimage=new FileOutputStream(file);
            }
            catch(FileNotFoundException e)
            {
                e.printStackTrace();
            }

                Blob image=result.getBlob("photo");
                inputimage=image.getBinaryStream();
                int size;
                try{
                    while((size=inputimage.read(buffer))!=-1)
                    {
                     outputimage.write(buffer, 0, size);
                    }
                }
                    catch (IOException e) {
                    e.printStackTrace();
                    }

                outputimage.close();
                inputimage.close();

        Image imgTemp=Toolkit.getDefaultToolkit().createImage(path);
        DeliveryDesk.getInstance().getButtonModel().getPhoto().setIcon(new ImageIcon(imgTemp));

        imgTemp=Toolkit.getDefaultToolkit().createImage("D:\\TP_BUFFER\\approved.jpg");
        DeliveryDesk.getInstance().getButtonModel().getBackLabel().setIcon(new ImageIcon(imgTemp));
        DeliveryDesk.getInstance().getButtonModel().getBackLabel().repaint();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        Image imgTemp=Toolkit.getDefaultToolkit().createImage("D:\\TP_BUFFER\\error.jpg");
        DeliveryDesk.getInstance().getButtonModel().getPhoto().setIcon(new ImageIcon(imgTemp));
        }
    }

    public void setNull()
    {
        try{
        Image imgTemp=Toolkit.getDefaultToolkit().createImage("D:\\TP_BUFFER\\error.jpg");
        DeliveryDesk.getInstance().getButtonModel().getPhoto().setIcon(new ImageIcon(imgTemp));

        imgTemp=Toolkit.getDefaultToolkit().createImage("D:\\TP_BUFFER\\denied.jpg");
        DeliveryDesk.getInstance().getButtonModel().getBackLabel().setIcon(new ImageIcon(imgTemp));
        DeliveryDesk.getInstance().getButtonModel().getBackLabel().repaint();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }


}
